/**
 * Created by Aber on 17/3/23.
 */
require('../../directives/base/mybase.scss');
require('./note.scss');

var app =require('../../directives/angular/app.js');

require('../../directives/editor_modles/wangEditor/wangEditor.js')(app);
require('../../directives/editor_modles/ueditor/ueditor.js')(app);

require('../../directives/header-nav/header-nav.js')(app);//引入指令(directive)文件
require('../../directives/base_content/content-suo/content-suo.js')(app);
require('../../directives/base_content/content-footer/content-footer.js')(app);

require('../../directives/user_modles/user/user.js')(app);
require('../../directives/user_modles/user_message/user_message.js')(app);
require('../../directives/user_modles/user_analysis/user_analysis.js')(app);

require('../../directives/note-modles/note-show/note-show.js')(app);
require('../../directives/note-modles/note-search/note-search.js')(app);
require("../../directives/note-modles/noted/noted")(app);

require('../../directives/task_modles/task_show/task_show')(app);

require('../../directives/wraps-best/wraps-best.js')(app);
require("../../directives/angular_tree/ang-tree")(app);
require("../../directives/team_modles/team/team")(app);

require("../../directives/lun_modles/lun/lun")(app);
if(!sessionStorage.user){
    location.href="../login/login.html";
}
app.controller('noteController', ['$scope','$http', function($scope,$http) {
    $scope.treedata={
        // title:"angularJS",
        // id:"",
        // data:
        // [
        //     {label: "Documents", type: "folder", children: [
        //         {label: "a picture", type: "pic"},
        //         {label: "another picture", type: "pic"},
        //         {label: "a doc", type: "doc"},
        //         {label: "a file", type: "file"},
        //         {label: "a movie", type: "movie",children: [
        //             {label: "a picture", type: "pic"},
        //             {label: "another picture", type: "pic"},
        //             {label: "a doc", type: "doc"},
        //             {label: "a file", type: "file"},
        //             {label: "a movie", type: "movie"}
        //         ]}
        //     ]
        // },
        //     {label: "My Computer", type: "folder", children: [
        //         {label: "email", type: "email"},
        //         {label: "home", type: "home"},
        //         {label: "a movie", type: "folder",children: [
        //             {label: "a picture", type: "pic"},
        //             {label: "another picture", type: "pic"},
        //             {label: "a doc", type: "doc"},
        //             {label: "a file", type: "file"},
        //             {label: "a movie", type: "movie"}
        //         ]}
        //     ]},
        //     {label: "trash", type: "trash"}
        // ]
    };
    $scope.wrapdata=[
        {"title":"个人笔记","templete":"noted",id:"s1"},
        // {"title":"团队笔记","templete":"team",id:"s2"},
        // {"title":"论坛笔记","templete":"lun",id:"s3"},
        // {"title":"单个笔记","templete":"ueditor",id:"s5"}
    ];
    $scope.noted=[
        // {
        //     "picture":"http://img1.3lian.com/2015/a1/122/d/75.jpg",
        //     "title":"美甲流行大窃密",
        //     "abstract":"美容一词可以从两个角度来理解。首选是“容”这个字，其次是“美”。“容”包括脸、仪态、和修饰三层意思。 “美”则具有形容词和动词的两层含义。形容词表明的是美容的结果和目的是美丽的好看的；动词则表明的是美容的过程，即美化和改变的意思。"
        // }
    ]
    $scope.setWrapData=function() {
        return {
            add:function (d) {
                var w=$scope.wrapdata;
                if(w.length +1 >= 7){
                    layer.msg("导航条限制最多6条")
                    return
                }
                var center=-1;
                for(var i=0;i<w.length;i++){
                    if(w[i].id==d.id){
                        center=i;
                    }else{
                        $("#slide-"+i+"-trigger").removeAttr("checked");
                    }
                }
                if(center!=-1){
                    layer.msg("该目录已打开")
                    $("#slide-"+center+"-trigger").attr("checked",true)
                }else{
                    $scope.wrapdata.push(d)
                }
            },
            remove:function (d) {
                $scope.wrapdata.splice(d,1)
            }
        }
    }()
    $scope.setTreeData=function () {
            return {
                change:function (d) {
                    $scope.treedata=JSON.parse(d);
                },
                changeTitle:function (d) {
                    $scope.treedata=d;
                },
                update:function () {
                    $http({
                        method:"post",
                        url:'/notes/detail',
                        data:{data:{listid:$scope.treedata.id}}
                    }).then(function(data) {
                        var temp=data.data.result;
                        console.log($scope.treedata)
                        temp.list=JSON.stringify($scope.treedata);
                        $http({
                            method:"post",
                            url:'/notes/update',
                            data:{data:temp}
                        }).then(function(data) {
                            if(data.data.msg.type==1){
                                layer.msg("ok");
                            }
                        }).catch(function(data) {
                            layer.msg("更新系统错误")
                        });
                    }).catch(function(data) {
                        layer.msg("拉取系统错误")
                    });
                }
            }
    }()

    $scope.noteclose=function (d) {
        $scope.setWrapData.remove(d)
    }
    $scope.noteChange=function (id) {
        var not="s1s2s3"
        if(id!='s1'&&id!='s2'&&id!='s3'){
            //请求获取最新list
            $http({
                method:"post",
                url:'/notes/detail',
                data:{data:{listid:id}}
            }).then(function(data) {
                if(data.data.result)
                {
                    $scope.setTreeData.change(data.data.result.list);
                }
            }).catch(function(data) {
                layer.alert('Request faile')
            });
        }else{
            $scope.treedata={};
        }
    }
}])